<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class News extends CI_Controller {
	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -  
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in 
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see http://codeigniter.com/user_guide/general/urls.html
	 */
    public function __construct(){
        parent::__construct();
        $this->load->model('Product_model','Product');
        $this->load->model('News_model','news');
        $this->load->library('pagination');
    }

	public function index()
	{	
	    $this->_utility("新闻动态 ");
		$config['uri_segment'] = 2;//参数三为当前页
		$config['base_url'] = site_url('news');
		$config['total_rows'] =$this->db->count_all('News');
		$config['per_page'] = '6'; 		
		$uri[2]=$this->uri->segment(2);
		
		$total=ceil($config['total_rows']/$config['per_page']);
		 if($uri[2]>$total){
		 	show_404();
		 }
		
		$this->pagination->initialize($config); 	
		
		$list=$this->news->getallnews($config['per_page'],$uri[2]);		
		
		$pager=$this->pagination->create_links();	
		
		$this->MySmarty->assign('pager',$pager);  
        $this->MySmarty->assign('list',$list); 
		$in_cache_name=md5("news".$uri[2]);
        $this->MySmarty->display("news.html",$in_cache_name,$this->Config['NewsTitle'],$this->Config['NewsKeywords'],$this->Config['NewsDescription']);

    }

	
	public function id()
	{
	 	$this->_utility();
	 	$uri[3]=$this->uri->segment(3);
		if(!is_numeric($uri[3])){show_404();}
	 	$rs=$this->news->getonenews($uri[3]);
	 	$this->MySmarty->assign('webtitle',$rs['Title']);  
	 	$rand=$this->news->getrandnews(4);
	 	$in_cache_name=md5("newsdetail".$uri[3]);
	 	
		$this->MySmarty->assign('rs',$rs);
		$this->MySmarty->assign('rand',$rand);

        $this->MySmarty->display("news_detail.html",$in_cache_name,$rs['Title'],$this->Config['NewsKeywords'],$this->Config['NewsDescription']);

    }


	
	function _utility($tltle=""){
		$topnews=$this->news->gethotnews(8);
        $newsRows=array();
        foreach($topnews as $key => $val){
           $newsRows[$key]['newsId']=$val['aid'];
            $newsRows[$key]['title']=$val['title'];
        }
		$this->MySmarty->assign('categoryname',"| 全部资讯");
		$this->MySmarty->assign('topnews',$newsRows);

	}
	

}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */